﻿
using GameFramework.Fsm;
using GameFramework.Procedure;
using GameFramework.Event;
using UnityGameFramework.Runtime;
/// <summary>
/// 大厅
/// </summary>
public class LobbyProcedure : GameBaseProcedure
{
    private LobbyForm lobbyForm;
    protected override void OnEnter(IFsm<IProcedureManager> procedureOwner)
    {
        base.OnEnter(procedureOwner);
        Log.Info("大厅");

        EntityInst.UI.OpenUIForm(AssetUtility.GetUIForm("LobbyForm"), "Default");

        EntityInst.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnUIFormOpenSuccessed);
        EntityInst.Event.Subscribe(OpenUIFormFailureEventArgs.EventId, OnUIFormOpenFailure);
    }

    protected override void OnLeave(IFsm<IProcedureManager> procedureOwner, bool isShutdown)
    {
        base.OnLeave(procedureOwner, isShutdown);

        EntityInst.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnUIFormOpenSuccessed);
        EntityInst.Event.Unsubscribe(OpenUIFormFailureEventArgs.EventId, OnUIFormOpenFailure);

        EntityInst.UI.CloseUIForm(lobbyForm.UIForm);
    }


    private void OnUIFormOpenSuccessed(object sender, GameEventArgs args)
    {
        OpenUIFormSuccessEventArgs ne = args as OpenUIFormSuccessEventArgs;
        Log.Info("LoginFormOpen successed");
        lobbyForm = ne.UIForm.Logic as LobbyForm;
        lobbyForm.PveGameEvent += StartPvpGame;
        lobbyForm.PvpGameEvent += StartPveGame;

    }
    private void OnUIFormOpenFailure(object sender, GameEventArgs args)
    {
        OpenUIFormFailureEventArgs ne = args as OpenUIFormFailureEventArgs;
        Log.Error("LoginFormOpen failure:msg = " + ne.ErrorMessage);
    }

    private void StartPvpGame()
    {
        Log.Info("PvpGame start");

        ChangeState<PvpAgainstProcedure>(base.mProcedureOwner);
    }
    private void StartPveGame()
    {
        Log.Info("PveGame start");

        ChangeState<PveAgainstProcedure>(base.mProcedureOwner);
    }
}
